<?php

namespace app\api\controller;

use app\common\model\UserScoreCount;
use think\Cache;
use think\Db;

class Quizzes extends \app\common\controller\Api
{
    protected $noNeedRight = '*';
    public function count(){
        //答题次数显示
        $user=$this->auth->getUser();
//        $count=Cache::get('quizzes_count_'.$user->id)?Cache::get('quizzes_count_'.$user->id):0;
//        $data['count']=config('site.answer_number')-$count;
//        $data['count']= $data['count']>0? $data['count']:0;
        $data['count']= UserScoreCount::getTodayCount($user->id);
        $this->success('答题次数',$data);
    }
    //问答列表
    public function index(){
        $user=$this->auth->getUser();
        //答题次数拦截
        if(UserScoreCount::getTodayCount($user->id)<=0){
            $this->error('今日答题已用完');
        }
        $quizzes= \app\common\model\Quizzes::field('id,question,answer_json,correct')->limit(6)->orderRaw("RAND()")->select();
        $quizzes_ids= array_column($quizzes,'correct','id');
        Cache::set('quizzes_'.$user->id,$quizzes_ids,60*30);
        $collection = collection($quizzes);
        $quizzes = $collection->hidden(['correct']);
        $this->success('竞答列表',$quizzes);
    }
    //问答详情
    public function details(){
        $quizzes= \app\common\model\Quizzes::field('id,question,answer_json')->find(2);
        $this->success('竞答详情',$quizzes);
    }
    //检测答案
    public function check(){
        $params = $this->request->post();
        if(empty($params['id'])||empty($params['answer'])){
            $this->error('参数错误');
        }
        $quizzes= \app\common\model\Quizzes::find($params['id']);
        if(!$quizzes){
            $this->error('问题不存在');
        }
        $data['is_correct']=$quizzes->correct==$params['answer']?true:false;
        $data['correct']=$quizzes->correct;
        $data['score']=$data['is_correct']?$quizzes->score:0;
        $this->success('答案检测',$data);
    }
    //提交答案
    public function submit(){
        $params=$this->request->post();
        $user=$this->auth->getUser();
        if(empty($params['answer'])){
            $this->error('参数错误');
        }
        if(UserScoreCount::getTodayCount($user->id)<=0){
            $this->error('今日答题已用完');
        }

        if(!$quizzes_list= Cache::get('quizzes_'.$user->id)){
            $this->error('答题超时或已提交');
        }
        $true_ids=[];
        foreach ($params['answer'] as $key=>$answer){
            if(array_key_exists($answer['id'],$quizzes_list)){
                if($quizzes_list[$answer['id']]==$answer['answer']){
                    $true_ids[]=$answer['id'];
                }
            }else{
                $this->error('异常提交');
            }
        }
        $score=\app\common\model\Quizzes::where('id','in',$true_ids)->sum('score');
        Db::startTrans();
        try{
            \addons\shopro\model\User::score($score,$user,'quizzes');
            $user=\app\common\model\User::where('id',$user->id)->find();
            UserScoreCount::setTodayCount($user->id);
            Cache::rm('quizzes_'.$user->id);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('提交成功',['score'=>$score,'total_score'=>$user->score]);
    }
}